*** Settings ***
Documentation     【接口信息】
...
...               【作者】
...               王慧彬
...
...               【时间】
...               2022-01-12
Suite Setup       Set Log Level    trace
Resource          ../../ConfigFiles/Resources_Config.robot

*** Variables ***

*** Test Cases ***
Class_01_注册账号
    [Tags]    uat    smoke    prod
    #构建请求参数
    ${random}    Evaluate    random.randint(1,999999)    random
    ${username}    Set Variable    names${random}
    &{datas}    Create Dictionary    username=${username}    password=123456    password_confirm=123456    email=${random}@${random}.com
    #发送请求
    ${response}    post request    ${be_register}    data=${datas}
    #校验结果
    Should Be Equal As Strings    ${response["code"]}    ${201}
    Should Be Equal As Strings    ${response["body"]["username"]}    ${username}

Exception_01_username为空
    [Tags]    uat    smoke
    #构建请求参数
    ${random}    Evaluate    random.randint(1,999999)    random
    ${username}    Set Variable    names${random}
    &{datas}    Create Dictionary    username=    password=123456    password_confirm=123456    email=${random}@${random}.com
    #发送请求
    ${response}    post request    ${be_register}    data=${datas}
    #校验结果
    Should Be Equal As Strings    ${response["code"]}    ${400}
    Should Be Equal As Strings    ${response["body"]["username"]}    ['该字段不能为空。']

Exception_02_密码为空
    [Tags]    uat    smoke
    #构建请求参数
    ${random}    Evaluate    random.randint(1,999999)    random
    ${username}    Set Variable    names${random}
    &{datas}    Create Dictionary    username=${username}    password=    password_confirm=123456    email=${random}@${random}.com
    #发送请求
    ${response}    post request    ${be_register}    data=${datas}
    #校验结果
    Should Be Equal As Strings    ${response["code"]}    ${400}
    Should Be Equal As Strings    ${response["body"]["password"]}    ['该字段不能为空。']

Exception_03_确认密码为空
    [Tags]    uat    smoke
    #构建请求参数
    ${random}    Evaluate    random.randint(1,999999)    random
    ${username}    Set Variable    names${random}
    &{datas}    Create Dictionary    username=${username}    password=123456    password_confirm=    email=${random}@${random}.com
    #发送请求
    ${response}    post request    ${be_register}    data=${datas}
    #校验结果
    Should Be Equal As Strings    ${response["code"]}    ${400}
    Should Be Equal As Strings    ${response["body"]["password_confirm"]}    ['该字段不能为空。']

Class_02_邮件为空正常注册
    [Tags]    uat    smoke
    #构建请求参数
    ${random}    Evaluate    random.randint(1,999999)    random
    ${username}    Set Variable    names${random}
    &{datas}    Create Dictionary    username=${username}    password=123456    password_confirm=123456    email=
    #发送请求
    ${response}    post request    ${be_register}    data=${datas}
    #校验结果
    Should Be Equal As Strings    ${response["code"]}    ${201}
    Should Be Equal As Strings    ${response["body"]["username"]}    ${username}

Class_Exception_03_数据驱动注册
    [Template]    创建用户
    #注册用例
    #${EMPTY}    ${EMPTY}    ${EMPTY}    ${EMPTY}    [\'该字段不能为空。\']
    random666    111111    111111    randomEmailll    PASS
    ${EMPTY}    111111    111111    randomEmailll    ['该字段不能为空。']
    15813349621    111111    ${EMPTY}    randomEmailll    ['该字段不能为空。']
    15966666662    ${EMPTY}    111111    randomEmailll    ['该字段不能为空。']

*** Keywords ***
创建用户
    [Arguments]    ${username}    ${password}    ${password_confirm}    ${email}    ${err}
    ${name}    random6
    ${emails}    randomEmail
    ${username}    Run Keyword If    '${username}'=='random666'    Set Variable    ${name}
    ...    ELSE    Set Variable    ${username}
    ${email}    Run Keyword If    '${email}'=='randomEmailll'    Set Variable    ${emails}
    ...    ELSE    Set Variable    ${email}
    #构建请求参数
    &{datas}    Create Dictionary    username=${username}    password=${password}    password_confirm=${password_confirm}    email=${email}
    #发送请求
    ${response}    post request    ${be_register}    data=${datas}
    @{data_key_list}    read_dic_key    ${response["body"]}
    #校验结果判断接口code来判断是否成功成功VC等于pass
    ${VC}    Run Keyword If    ${response["code"]}==${201}    Set Variable    PASS
    ...    ELSE    log    Not 201
    #这块也是校验是否成功恶心的地方是本demo的成功也带了username参数所以判断比较多
    #先判断response有没有username KEY
    ${aaaa}    ${msg}    Run Keyword And Ignore Error    Dictionary Should Contain Key    ${response["body"]}    username
    # 如果有 在判断也就是aaaa=pass 在判断username 是否等于参数如果等于参数 说明用户创建成功VC还是pass
    # 如果有 但是username的值不是参数 则修改VC的值为返回值 也就是创建失败后username的错误内容
    ${name_Response}    Run Keyword If    "${aaaa}"=="PASS"    Get From Dictionary    ${response["body"]}    username
    ${VC}    Run Keyword If    "${name_Response}"=="${username}"    Set Variable    PASS
    ...    ELSE IF    "${name_Response}"=="None"    Set Variable    ${VC}
    ...    ELSE    Get From Dictionary    ${response["body"]}    username
    # 下面就判断其他参数了 比较简单 因为创建成功是没有key的 有说明就是失败了 该字段的错误提示
    ${aaaa}    ${msg}    Run Keyword And Ignore Error    Dictionary Should Contain Key    ${response["body"]}    password
    ${VC}    Run Keyword If    "${aaaa}"=="PASS"    Get From Dictionary    ${response["body"]}    password
    ...    ELSE    Set Variable    ${VC}
    ${aaaa}    ${msg}    Run Keyword And Ignore Error    Dictionary Should Contain Key    ${response["body"]}    password_confirm
    ${VC}    Run Keyword If    "${aaaa}"=="PASS"    Get From Dictionary    ${response["body"]}    password_confirm
    ...    ELSE    Set Variable    ${VC}
    ${aaaa}    ${msg}    Run Keyword And Ignore Error    Dictionary Should Contain Key    ${response["body"]}    email
    ${VC}    Run Keyword If    "${aaaa}"=="PASS"    Get From Dictionary    ${response["body"]}    email
    ...    ELSE    Set Variable    ${VC}
    Should Be Equal As Strings    ${VC}    ${err}
